package com.myk.game.gobangchess.expand.bwantispace;

import com.myk.game.gobangchess.constants.ColorCost;
import com.myk.game.gobangchess.signs.BadgeSign;
import com.myk.game.gobangchess.signs.LineSign;
import com.myk.game.gobangchess.signs.TitleSign;

/**
 * 棋子-怪物基类
 */
public abstract class BaseMonsterPiece extends BasePiece {

    protected int hp; //生命值
    protected int atk;//攻击力
    protected boolean isNewAppear; //是否新出现的
    protected int[] recentMovePath; //最近一次移动路径
    protected int[] recentAttackPath; //最近一次攻击路径

    public BaseMonsterPiece(Game game, String name, int row, int col) {
        super(game, name, row, col);
        isNewAppear = true;
    }

    //自身是否与humanPiece相邻
    public boolean isAdjacentToPiece(BasePiece piece) {
        if (this.col == piece.col) {
            return Math.abs(this.row - piece.row) == 1;
        }
        else if (this.row == piece.row) {
            return Math.abs(this.col - piece.col) == 1;
        }
        return false;
    }

    //移动行为
    protected void move(int row, int col) {
        game.helper.room.curGrids[this.row][this.col] = 0;
        int oldPos = game.helper.room.getIndex(this.row, this.col);
        this.row = row;
        this.col = col;
        game.helper.room.curGrids[row][col] = 7;
        int newPos = game.helper.room.getIndex(row, col);
        this.recentMovePath = new int[]{oldPos, newPos};
        this.recentAttackPath = null;
        this.isNewAppear = false;
    }

    //攻击行为
    protected void attack(Human human) {
        human.hp -= this.atk;
        int fromPos = game.helper.room.getIndex(this.row, this.col);
        int toPos = game.helper.room.getIndex(human.row, human.col);
        this.recentAttackPath = new int[]{fromPos, toPos};
        this.recentMovePath = null;
        this.isNewAppear = false;
    }

    @Override
    public void renderSelf() {
        //绘制棋子底座
        game.helper.room.curGrids[row][col] = 7;
        //新出生绘制
        if (isNewAppear) {
            game.helper.room.signList.add(new BadgeSign(game.helper.room.getIndex(row, col), "新", 7, GameValue.COLOR_RED, ColorCost.TRANSPARENT));
        }
        //绘制名称
        game.helper.room.signList.add(new TitleSign((col + 0.5f) / game.helper.room.curGrids[0].length, (row + 0.5f) / game.helper.room.curGrids.length, 0, name, GameValue.COLOR_BLACK, 0.3f));
        //绘制生命值
        game.helper.room.signList.add(new BadgeSign(game.helper.room.getIndex(row, col), hp + "", 1, GameValue.COLOR_GREEN, ColorCost.TRANSPARENT));
        //绘制移动路径
        if (recentMovePath != null) {
            game.helper.room.signList.add(new LineSign(recentMovePath[0], recentMovePath[1], GameValue.COLOR_GRAY_LIGHT, 1));
        }
        //绘制攻击路径
        if (recentAttackPath != null) {
            game.helper.room.signList.add(new LineSign(recentAttackPath[0], recentAttackPath[1], GameValue.COLOR_RED_LIGHT, 1));
        }
    }

    /**
     * 怪物自己简单思考进行行动
     */
    public abstract void updateSelf();

}